﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;

namespace AutoMeasureV2._1
{
    public class iniInfo
    {
        #region 声明读写INI文件的API函数
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);//写入INI函数 
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
            string key, string defVal, StringBuilder retVal, int size, string filePath);//读取INI函数

        /// <summary>
        /// ini文件读取
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string IniReadValue(string section, string key, string filePath)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(section, key, "", temp, 255, filePath);
            return temp.ToString();
        }

        #endregion

        private List<Communication> _communications = new List<Communication>();
        public List<Communication> Communications
        {
            get { return _communications; }
            set { _communications = value; }
        }

        private List<ResSin> _resSins = new List<ResSin>();
        public List<ResSin> ResSins = new List<ResSin>();
        public Dictionary<int, Band> _band = new Dictionary<int, Band>();
        Dictionary<string, string> files = new Dictionary<string, string>();

        string basePath = AppDomain.CurrentDomain.BaseDirectory + @"配置文件";
        string ResFile = AppDomain.CurrentDomain.BaseDirectory + @"配置文件\Res.ini";
        string BandFile = AppDomain.CurrentDomain.BaseDirectory + @"配置文件\Band.ini";

        /// <summary>
        /// 配置文件信息读取
        /// </summary>
        public void Info()
        {
            foreach (var file in Directory.GetFiles(basePath))
            {
                if (Path.GetFileNameWithoutExtension(file).ToUpper().IndexOf("BAND") > -1) continue;
                if (Path.GetFileNameWithoutExtension(file).ToUpper().IndexOf("RES") > -1) continue;

                files.Add(Path.GetFileNameWithoutExtension(file), file);
            }

            foreach (var file in files)
            {
                Communication _communication = new Communication();

                string Res_name = IniReadValue("Communication", "Name", file.Value);
                IPAddress Res_remoteIP = IPAddress.Parse(IniReadValue("Communication", "RemoteIP", file.Value));
                double Res_port = double.Parse(IniReadValue("Communication", "RemotePort", file.Value));
                string Res_sign = IniReadValue("Communication", "Sign", file.Value);

                _communication.Name = Res_name;
                _communication.RemoteIP = Res_remoteIP;
                _communication.RemotePort = Res_port;
                _communication.Sign = Res_sign;
                Communications.Add(_communication);
                int Res_count = int.Parse(IniReadValue("Info", "Num", file.Value));

                for (int i = 0; i < Res_count; i++)
                {
                    string section = "SIN" + Convert.ToString(i);
                    string obj_name = IniReadValue(section, "ObjName", file.Value);
                    string obj_ID = IniReadValue(section, "ObjID", file.Value);
                    double obj_order = double.Parse(IniReadValue(section, "ObjOrder", file.Value));
                    double obj_respond = double.Parse(IniReadValue(section, "ObjRespond", file.Value));
                    double obj_digit = double.Parse(IniReadValue(section, "ObjDigit", file.Value));
                    string obj_selected = IniReadValue(section, "ObjSelected", file.Value);

                    SIN _sin = new SIN();

                    _sin.objName = obj_name;
                    _sin.objID = obj_ID;
                    _sin.objOrder = obj_order;
                    _sin.objRespond = obj_respond;
                    _sin.objDigit = obj_digit;

                    _communication.Sins.Add(_sin);
                }
            }

            //资源信息读取
            int mes_Count = int.Parse(IniReadValue("Info", "Num", ResFile));
            for (int i = 0; i < mes_Count; i++)
            {
                ResSin _sin = new ResSin();
                string section = "SIN" + Convert.ToString(i);
                string mes_Name = IniReadValue(section, "Name", ResFile);
                string mes_ID = IniReadValue(section, "UseID", ResFile);
                int mes_Num = int.Parse(IniReadValue(section, "UseNum", ResFile));
                string mes_Selected = IniReadValue(section, "Selected", ResFile);

                _sin.Name = mes_Name;
                _sin.Num = mes_Num;
                _sin.ID = mes_ID;

                ResSins.Add(_sin);
            }

            //绑定信息读取
            int ban_Count = int.Parse(IniReadValue("Info", "Num", BandFile));
            for (int i = 0; i < ban_Count; i++)
            {
                Band _bandsin = new Band();
                string section = "BAND" + Convert.ToString(i);
                string messageName = IniReadValue(section, "MessageName", BandFile);
                string messageID = IniReadValue(section, "MessageID", BandFile);
                string objName = IniReadValue(section, "ObjName", BandFile);
                IPAddress objIP = IPAddress.Parse(IniReadValue(section, "ObjIP", BandFile));
                double objPort = double.Parse(IniReadValue(section, "ObjPort", BandFile));
                string objSign = IniReadValue(section, "ObjSign", BandFile);
                string objID = IniReadValue(section, "ObjID", BandFile);
                double objOrder = double.Parse(IniReadValue(section, "ObjOrder", BandFile));
                double objRespond = double.Parse(IniReadValue(section, "ObjRespond", BandFile));
                double objDigit = double.Parse(IniReadValue(section, "ObjDigit", BandFile));

                _bandsin.MesName = messageName;
                _bandsin.MesID = messageID;
                _bandsin.ObjName = objName;
                _bandsin.ObjIP = objIP;
                _bandsin.ObjID = objID;
                _bandsin.ObjOrder = objOrder;
                _bandsin.ObjRespond = objRespond;
                _bandsin.ObjSign = objSign;
                _bandsin.ObjPort = objPort;
                _bandsin.ObjDigit = objDigit;

                _band.Add(i, _bandsin);
                //Bands.Add(_bandsin);
            }
        }

        /// <summary>
        /// 向绑定文件内添加绑定信息
        /// </summary>
        /// <param name="band">写入band.ini文件的信息列</param>
        public void AddBand(Band band)
        {
            int i = _band.Count;
            _band.Add(i, band);

            foreach (var item in _band)
            {
                string section = "BAND" + Convert.ToString(item.Key);
                WritePrivateProfileString(section, "MessageName", item.Value.MesName, BandFile);
                WritePrivateProfileString(section, "MessageID", item.Value.MesID, BandFile);
                WritePrivateProfileString(section, "ObjName", item.Value.ObjName, BandFile);
                WritePrivateProfileString(section, "ObjIP", Convert.ToString(item.Value.ObjIP), BandFile);
                WritePrivateProfileString(section, "ObjPort", Convert.ToString(item.Value.ObjPort), BandFile);
                WritePrivateProfileString(section, "ObjSign", item.Value.ObjSign, BandFile);
                WritePrivateProfileString(section, "ObjID", item.Value.ObjID, BandFile);
                WritePrivateProfileString(section, "ObjOrder", Convert.ToString(item.Value.ObjOrder), BandFile);
                WritePrivateProfileString(section, "ObjRespond", Convert.ToString(item.Value.ObjRespond), BandFile);
                WritePrivateProfileString(section, "ObjDigit", Convert.ToString(item.Value.ObjDigit), BandFile);
            }

            WritePrivateProfileString("Info", "Num", Convert.ToString(i + 1), BandFile);
        }

        /// <summary>
        /// 从绑定文件里删除指定绑定信息
        /// </summary>
        /// <param name="resName">删除资源名称为：resName的绑定信息</param>
        public void removeBand(string resName)
        {
            int i = _band.Count;
            if (i != 0)
            {
                foreach (var item in _band)
                {
                    if (resName == item.Value.MesName)
                    {
                        _band.Remove(item.Key);
                        break;
                    }
                }
                WritePrivateProfileString("Info", "Num", Convert.ToString(i - 1), BandFile);
                //删除最后一项，重写绑定信息
                string section = "BAND" + Convert.ToString(i - 1);
                WritePrivateProfileString(section, null, null, BandFile);

                int k = 0;
                if (_band != null)
                {
                    foreach (var items in _band)
                    {
                        string bsection = "BAND" + Convert.ToString(k);
                        WritePrivateProfileString(bsection, "MessageName", items.Value.MesName, BandFile);
                        WritePrivateProfileString(bsection, "MessageID", items.Value.MesID, BandFile);
                        WritePrivateProfileString(bsection, "ObjName", items.Value.ObjName, BandFile);
                        WritePrivateProfileString(bsection, "ObjIP", Convert.ToString(items.Value.ObjIP), BandFile);
                        WritePrivateProfileString(bsection, "ObjPort", Convert.ToString(items.Value.ObjPort), BandFile);
                        WritePrivateProfileString(bsection, "ObjSign", items.Value.ObjSign, BandFile);
                        WritePrivateProfileString(bsection, "ObjID", items.Value.ObjID, BandFile);
                        WritePrivateProfileString(bsection, "ObjOrder", Convert.ToString(items.Value.ObjOrder), BandFile);
                        WritePrivateProfileString(bsection, "ObjRespond", Convert.ToString(items.Value.ObjRespond), BandFile);
                        WritePrivateProfileString(bsection, "ObjDigit", Convert.ToString(items.Value.ObjDigit), BandFile);
                        k++;
                    }
                }
            }
        }

        /// <summary>
        /// 清空绑定文件中的绑定信息
        /// </summary>
        public void clear()
        {

            foreach (var item in _band)
            {
                string section = "BAND" + Convert.ToString(item.Key);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
                WritePrivateProfileString(section, null, null, BandFile);
            }
            _band.Clear();
            WritePrivateProfileString("Info", "Num", "0", BandFile);
        }
    }

    /// <summary>
    /// 操作对象通讯信息
    /// </summary>
    public class Communication
    {
        public string Name { get; set; }                  //设备名称
        public IPAddress RemoteIP { get; set; }           //远程地址
        public double RemotePort { get; set; }            //远程端口
        public string Sign { get; set; }                  //识别码

        private List<SIN> _sins = new List<SIN>();

        public List<SIN> Sins
        {
            get { return _sins; }
            set { _sins = value; }
        }
    }

    /// <summary>
    /// 操作对象序列
    /// </summary>
    public class SIN
    {
        public string objName { get; set; }                //操作对象名称
        public string objID { get; set; }                  //操作对象编码
        public double objOrder { get; set; }               //操作指令序号
        public double objRespond { get; set; }             //操作响应端口号
        public double objDigit { get; set; }               //操作对象数据类型

    }

    /// <summary>
    /// 资源信息序列
    /// </summary>
    public class ResSin
    {
        public string Name { get; set; }         //资源名称
        public string ID { get; set; }           //资源编号
        public int Num { get; set; }             //资源记号

    }

    /// <summary>
    /// 绑定信息序列
    /// </summary>
    public class Band
    {
        public string MesName { get; set; }       //资源名称
        public string MesID { get; set; }         //资源编号
        public string ObjName { get; set; }       //对象名称
        public IPAddress ObjIP { get; set; }      //操作对象地址
        public double ObjPort { get; set; }       //操作端口号
        public string ObjSign { get; set; }       //操作对象识别码
        public string ObjID { get; set; }         //操作对象编码
        public double ObjOrder { get; set; }      //操作对象指令号
        public double ObjRespond { get; set; }    //操作对象响应端口号
        public double ObjDigit { get; set; }      //操作对象数据类型

    }
}

